package com.robot.controller;


import com.robot.common.enums.IsDeleteEnum;
import com.robot.dao.dto.KnowledgeDTO;
import com.robot.dao.dto.KnowledgePageQueryDTO;
import com.robot.dao.dto.KnowledgePageQueryFileDTO;
import com.robot.common.result.PageResult;
import com.robot.common.result.Response;
import com.robot.dao.entity.Knowledge;
import com.robot.service.FileService;
import com.robot.service.KnowledgeService;
import com.robot.service.UserKnowledgeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/knowledge")
@Api(tags = "知识库相关接口")
public class KnowledgeController {
    @Autowired
    private KnowledgeService knowledgeService;
    @Autowired
    private UserKnowledgeService userKnowledgeService;
    @Autowired
    private FileService fileService;

    @PostMapping("/create")
    @ApiOperation("新建知识库")
    public Response<Knowledge> create(@RequestBody KnowledgeDTO knowledgeDTO){
        Knowledge knowledge = knowledgeService.createknowledge(knowledgeDTO);
        return Response.success(knowledge);
    }

    @PostMapping("/pageQueryByUserIdAndCategory")
    @ApiOperation("根据用户id分页分类查询知识库")
    public Response<PageResult> page(@RequestBody KnowledgePageQueryDTO knowledgePageQueryDTO){
        PageResult pageResult =  knowledgeService.pageQuery(knowledgePageQueryDTO);
        return Response.success(pageResult);
    }

    @PostMapping("/pageQueryFilesByKnowledgeId")
    @ApiOperation("根据知识库id分页查询文件")
    public Response<PageResult> pageFilesByKnowledgeId(@RequestBody KnowledgePageQueryFileDTO knowledgePageQueryFileDTO){
        PageResult pageResult = knowledgeService.pageQueryfile(knowledgePageQueryFileDTO);
        return Response.success(pageResult);
    }

    @PostMapping("/delete")
    @ApiOperation("根据知识库id逻辑删除知识库")
    public Response<String> delete(@RequestBody KnowledgeDTO knowledgeDTO){
        knowledgeDTO.setIsDelete(IsDeleteEnum.DELETED.getValue());
        knowledgeDTO.setId(new Long(knowledgeDTO.getKnowledgeId()));
        knowledgeService.updateById(knowledgeDTO);

        fileService.deleteknowledgefile(new Long(knowledgeDTO.getKnowledgeId()));

        return Response.success("逻辑删除知识库成功");
    }
}
